#include <math.h>
#include <intrins.h>

#include "HS52F2003.h"
#include "RTC8563S.h"



#define  PORT_RTC8563S_SDA  P1_3
#define  PORT_RTC8563S_SCL  P1_2


#define  I2C_SCL_0()   PORT_RTC8563S_SCL=0;
#define  I2C_SCL_1()   PORT_RTC8563S_SCL=1;

#define  I2C_SDA_0()   PORT_RTC8563S_SDA=0;
#define  I2C_SDA_1()   PORT_RTC8563S_SDA=1;


void i2c_delay(void) {
//	u8 i;
//	for (i = 0; i < 1; ++i);
}
void RTC8563S_IO_init() {



	//P23 P24 开漏输出 
	P1ON |= 0x0C;//P1ON P1OD
	
	P1PH |= 0x0C;

	


}


void i2c_start(void);
void i2c_stop(void);
u8   i2c_test_ack(void);
void i2c_ack(void);
void i2c_no_ack(void);
void i2c_delay(void);
void i2c_send_byte(u8 ucdata);

u8 i2c_read_byte(void);


u8 i2c_ReadByte(void) {
	u8 i;
	u8 value;

	/* 读到第1个bit为数据的bit7 */
	value = 0;
	for (i = 0; i < 8; i++) {
		value <<= 1;
		I2C_SCL_1();
		i2c_delay();
		if (PORT_RTC8563S_SDA) {
			value++;
		}
		I2C_SCL_0();
		i2c_delay();
	}
	return value;
}



void i2c_start(void) {
	I2C_SDA_1();
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SDA_0();
	i2c_delay();
	I2C_SCL_0();

}

void i2c_stop(void) {
	I2C_SDA_0();
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SDA_1();

}


u8   i2c_test_ack(void) {
	I2C_SDA_1();
	i2c_delay();

	I2C_SCL_1();
	i2c_delay();

	I2C_SCL_0();
//	i2c_delay();
	return 0;
}

void i2c_ack(void) {
	I2C_SDA_0();
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();
	I2C_SDA_1();


}
void i2c_no_ack(void) {
	I2C_SDA_1();
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();

}



void i2c_send_byte(u8 ucdata) {
	u8 i = 0;


	if (ucdata & 0x80) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	if (ucdata & 0x40) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();
	if (ucdata & 0x20) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();
	if (ucdata & 0x10) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	if (ucdata & 0x08) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	if (ucdata & 0x04) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();
	if (ucdata & 0x02) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();
	if (ucdata & 0x01) {
		I2C_SDA_1();
	}
	else {
		I2C_SDA_0();
	}
	i2c_delay();
	I2C_SCL_1();
	i2c_delay();
	I2C_SCL_0();
	i2c_delay();



	I2C_SDA_1();
}

u8 I2C_ReadByte(u8 ReadAddress){
	
	
	unsigned  char read_data;
	i2c_start();
	i2c_send_byte(0xA2);
	i2c_test_ack();
	i2c_send_byte(ReadAddress);
	i2c_test_ack();
	i2c_stop();
	
	i2c_start();
	i2c_send_byte(0xA3);
	i2c_test_ack();
	read_data = i2c_ReadByte();
	i2c_test_ack();//i2c_no_ack
	i2c_stop();
	
	return read_data;
	
	
	
	
	
	
//	i2c_start();
//	i2c_send_byte(DeviceAddress);
//	i2c_test_ack();
//	i2c_send_byte(WriteAddress);
//	i2c_test_ack();

}

void rtc_WriteByte(u8 WriteAddress, u8 SendByte) {

	i2c_start();
	i2c_send_byte(0xA2);
	i2c_test_ack();
	i2c_send_byte(WriteAddress);
	i2c_test_ack();
	i2c_send_byte(SendByte);
	i2c_test_ack();
	i2c_stop();
}


void I2C_WriteByte(u8 DeviceAddress, u8 WriteAddress, u8 SendByte) {

	i2c_start();
	i2c_send_byte(DeviceAddress);
	i2c_test_ack();
	i2c_send_byte(WriteAddress);
	i2c_test_ack();
	i2c_send_byte(SendByte);
	i2c_test_ack();
	i2c_stop();
}


static void delay_ms(unsigned char ms) {
	unsigned char i = 0;

	while (ms--) {
		for (i = 0; i < 100; i++);

	}
}


//CRC校验类型：CRC8
//多项式：X8+X5+X4+1
//Poly:0011 0001 0x31
unsigned char Calc_CRC8(unsigned char *message,unsigned char Num)
{
	unsigned char i;
	unsigned char byte;
	unsigned char crc =0xFF;
	for (byte = 0;byte<Num;byte++)
	{
		crc^=(message[byte]);
		for(i=8;i>0;--i)
		{
			if(crc&0x80)
				crc=(crc<<1)^0x31;
			else
				crc=(crc<<1);
		}
	}
	return crc;
}

void aht20_start(void) {

	i2c_start();
	i2c_send_byte(0x70);
	i2c_test_ack();
	i2c_send_byte(0xAC);
	i2c_test_ack();
	i2c_send_byte(0x33);
	i2c_test_ack();
	i2c_send_byte(0x00);
	i2c_test_ack();
	i2c_stop();
}

unsigned char aht_buf[7];

int tempture_val;
unsigned char humidity_val;

bit f_tempture_mode;
void cal_tempture_humidity()
{
	unsigned long humidity = (((unsigned int)aht_buf[1]<<8)|aht_buf[2]);
	unsigned char tempture_H = (aht_buf[3]<<4)|(aht_buf[4]>>4);
	unsigned char tempture_L = (aht_buf[4]<<4)|(aht_buf[5]>>4);
	
	long tempture = ((long)tempture_H<<8)|tempture_L;
		
	humidity_val = humidity*100/65535;
	
	if(!f_tempture_mode)//摄氏度
	{
		tempture_val = ((tempture*200*10)/65535) - 500;
	}
	else//华摄氏度
	{
//		tempture_val = (((tempture*200*10)/65535) - 500)*1.8 + 320;
		tempture_val = ((tempture*200*18)/65535) - 900 + 320;
	}
	
	
}

//extern bit f_sensor_err;
void aht20_read(void) {

	i2c_start();
	i2c_send_byte(0x71);
	i2c_test_ack();
	aht_buf[0] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[1] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[2] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[3] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[4] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[5] = i2c_ReadByte();
	i2c_ack();
	
	aht_buf[6] = i2c_ReadByte();
	i2c_no_ack();
	i2c_stop();
	
	if((Calc_CRC8(aht_buf, 6) == aht_buf[6]) && (aht_buf[0] != 0xFF) )
	{
		cal_tempture_humidity();
//		f_sensor_err=0;
		return;
//		return 0;
	}
//	f_sensor_err=1;
//	return 1;
}



//void IS31FL3743A_Init(void) {
//	u8 i;
//
//	//soft rst
//	I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc0);
//	I2C_WriteByte(Addr_GND_GND, 0x2F, 0xAE);
//	delay_ms(3);
//
//
//	/* 2. enable chipen and set 0x00 0xb1 number */
//	I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc0);
//	I2C_WriteByte(Addr_GND_GND, 0x00, 0xFF);
//
//	I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc0);
//	I2C_WriteByte(Addr_GND_GND, 0x29, 0x00);
//
//	//I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc1);
//	////I2C_WriteByte(Addr_GND_GND, 0xfd, 0x00); //page pwm
//
//	//for (i = 0; i < 0xC6; i++) {
//	//	I2C_WriteByte(Addr_GND_GND, i, 0x00);
//	//}
//
//
//
//
//	//I2C_WriteByte(Addr_GND_GND, 0xfe, 0xc5); //unlock
//
//	////I2C_WriteByte(Addr_GND_GND, 0xfd, 0x01); //page scaling
//
//	//for (i = 1; i < 0xC7; i++) {
//	//	I2C_WriteByte(Addr_GND_GND, i, 0xFF); //Scaling
//	//}
//
//
//	I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc0);
//	I2C_WriteByte(Addr_GND_GND, 0x01, 0xFF); //GCC
//
//	I2C_WriteByte(Addr_GND_GND, 0xf0, 0xc3);
//
//
//	for (i = 0; i < 0x42; i++) {
//		I2C_WriteByte(Addr_GND_GND, i, 0); //white
//	}
//
//
//
//	//SL page
//	I2C_WriteByte(Addr_GND_GND, 0xF0, 0xC2);
//	for (i = 0; i < 198; i++) {
//		I2C_WriteByte(Addr_GND_GND, i, 0xFF); //white
//	}
//
//
//
//	//SL page
//	I2C_WriteByte(Addr_GND_GND, 0xF0, 0xC1);
//	for (i = 0; i < 198; i++) {
//		I2C_WriteByte(Addr_GND_GND, i, 0); //white
//	}
//
//
//
//	//	I2C_WriteByte(Addr_GND_GND, 0x46, 0x04);
//}
//
//u8 IS31FL3743A_Mode_5(void)   //Rainbow wave
//{
//	u8 i;
//
//
//
//
//	// PWM  page
//	I2C_WriteByte(Addr_GND_GND, 0xF0, 0xC1);
//	//I2C_WriteByte(Addr_GND_GND, 0xFD, 0x00);
//	for (i = 0; i < 198; i++) {
//		I2C_WriteByte(Addr_GND_GND, i, 0xFF); //white
//	}
//
//	//I2C_WriteByte(Addr_GND_GND, 0xF0, 0xC2);
//	////I2C_WriteByte(Addr_GND_GND, 0xFD, 0x00);
//	//for (i = 0; i < 198; i++) {
//	//	I2C_WriteByte(Addr_GND_GND, i, 0xFF); //white
//	//}
//	//if(Mode_Num != 5)return 0;
//	//while (1);
//
//
//
//	return 1;
//}
//extern unsigned char xdata g_led_pwm_array[100];
//
//
//extern char xdata g_led_pwm_array2[198];
//
//void update_all_led() {
//
//	u8 i;
//	I2C_WriteByte(Addr_GND_GND, 0xF0, 0xC1); //Page1 
//
//	i2c_start();
//	i2c_send_byte(Addr_GND_GND);
//	i2c_test_ack();
//	i2c_send_byte(0);
//	i2c_test_ack();
//	for (i = 0; i < 198; i++) {
//		i2c_send_byte(g_led_pwm_array2[i]);
//		i2c_test_ack();
//	}
//	i2c_stop();
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(18);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[19 - i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(36);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[20 + i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(54);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[39 - i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(72);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[40 + i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(90);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[59 - i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(108);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[60 + i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(126);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[79 - i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(144);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[80 + i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//
//
////	i2c_start();
////	i2c_send_byte(Addr_GND_GND);
////	i2c_test_ack();
////	i2c_send_byte(162);
////	i2c_test_ack();
////	for (i = 0; i < 10; i++) {
////		i2c_send_byte(g_led_pwm_array[99 - i]);
////		i2c_test_ack();
////	}
////	i2c_stop();
//
//}
//
//
//u8 read_ID(void) {
//	unsigned  char xdata ID = 0;
//	I2C_WriteByte(0x40, 0xF0, 0xC0); //Page1 
//
//	i2c_start();
//	i2c_send_byte(Addr_GND_GND);
//	i2c_test_ack();
//	i2c_send_byte(0x2F);
//	i2c_test_ack();
//	i2c_stop();
//
//	i2c_start();
//	i2c_send_byte(Addr_GND_GND | 0x01);
//	i2c_test_ack();
//	ID = i2c_ReadByte();
//	i2c_test_ack();
//	i2c_stop();
//	return ID;
//}